<style type="text/css">
h3 { margin: 0px; margin-top: 12px; margin-bottom: 6px; }
code { color: #333366; font-style: italic; }
code.opt { font-weight: bold; font-style: normal; }
.error { color: #993333; }
.warning { color: #cc33cc; }
</style>
<?php
    
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    require_once('admin.php');
    
    $title = 'Details';
    $this_file = 'lp-stats/details.php';
    $parent_file = 'lp-stats/page-hits.php';
    
    $submenu[$this_file][] = array(
        $title,
        6,
        $this_file,
        "LightPress $title"
        );
    
    $this_url = get_option('siteurl') . '/wp-admin/admin.php?page=' . $this_file;
    
    $req_url = (isset($_GET['hiturl'])) ? rawurldecode($_GET['hiturl']) : '';
    $hit_url = (!empty($req_url)) ? $this_url . '&hiturl=' . rawurlencode($req_url) : $this_url;
    
    switch ((isset($_GET['sort'])) ? $_GET['sort'] : '') {
        case 'url':
            $sortorder = 'url, day, time, host';
            break;
        case 'host':
            $sortorder = 'host, day, time, url';
            break;
        case 'refer':
            $sortorder = 'referrer, refer_query, host, url';
            break;
        default:
            $sortorder = 'day, time, host, url';
            break;
    }
    
?>
<div class="wrap">

<?php
    if (!empty($message))
        echo '<div class="updated"><p><strong>' . $message . '.</strong></p></div>';

    //options menu
    $viewmodes = array('today', 'yesterday', 'week', 'month');
    
    //get current local time information
    $local_offset = get_option('gmt_offset');
    $local_time = time() + $local_offset*3600;
    $local_data = array('year'  => (int) gmdate('Y', $local_time),
                        'month' => (int) gmdate('m', $local_time),
                        'day'   => (int) gmdate('d', $local_time),
                        'hour'  => (int) gmdate('H', $local_time),
                        'minute'=> (int) gmdate('i', $local_time),
                        'second'=> (int) gmdate('s', $local_time));
    
    switch ( (isset($_GET['viewmode'])) ? $_GET['viewmode'] : '') {
        case 'week':
            $currentmode = 'week';
            $start_ts = gmmktime(-1 * $local_offset, 0, 0, $local_data['month'], $local_data['day']-6);
            $end_ts = $start_ts + 7*24*3600; // end of week
            $showdate = gmstrftime(get_option('lp_opt_short_date_format'), $start_ts + $local_offset*3600)
                        . ' &mdash ' .
                        gmstrftime(get_option('lp_opt_short_date_format'), $end_ts + $local_offset*3600);
            break;
        case 'month':
            $currentmode = 'month';
            $start_ts = gmmktime(-1 * $local_offset, 0, 0, $local_data['month'], 1);
            $end_ts = gmmktime(-1 * $local_offset, 0, 0, $local_data['month']+1, 1);
            $showdate = gmstrftime(get_option('lp_opt_short_date_format'), $start_ts + $local_offset*3600)
                        . ' &mdash ' .
                        gmstrftime(get_option('lp_opt_short_date_format'), $end_ts + $local_offset*3600);
            break;
        case 'yesterday':
            $currentmode = 'yesterday';
            $start_ts = gmmktime(-1 * $local_offset, 0, 0, $local_data['month'], $local_data['day']-1);
            $end_ts = $start_ts + 24*3600; // end of day
            $showdate = gmstrftime(get_option('lp_opt_short_date_format'), $start_ts + $local_offset*3600);
            break;
        default:
            $currentmode = 'today';
            $start_ts = gmmktime(-1 * $local_offset, 0, 0, $local_data['month'], $local_data['day']);
            $end_ts = $start_ts + 24*3600; // end of day
            $showdate = gmstrftime(get_option('lp_opt_short_date_format'), $start_ts + $local_offset*3600);
            break;
    }
    $hit_url .= '&viewmode=' . $currentmode;
    
    echo "<h2>Hit Details for : $showdate </h2>";
    
    if (!empty($req_url))
        echo ' <a href="' . $this_url . '" title="View All">View All</a>';
    foreach($viewmodes as $mode) {
        if ($mode == $currentmode)
            echo '<strong>' . ucfirst($mode) . '</strong>';
        else
            echo ' <a href="' . get_option('siteurl') . '/wp-admin/admin.php?page=' .
                $this_file . '&viewmode=' . $mode . '">' . ucfirst($mode) . '</a> ';
    }
    
?>

<table width="100%" cellspacing="3" cellpadding="3">
<tr>
    <th><a href="<?php echo $hit_url . '&sort=url'; ?>" title="Sort by URL">Request URL</a></th>
    <th><a href="<?php echo $hit_url . '&sort=refer'; ?>" title="Sort by referrer">Referrer</a></th>
    <th><a href="<?php echo $hit_url . '&sort=host'; ?>" title="Sort by host">Host</a></th>
    <th><a href="<?php echo $hit_url . '&sort=time'; ?>" title="Sort by time">Time</a></th>
</tr>

<?php
    $datelist = '';
    $nextday = $start_ts + 24*3600;
    while ($nextday < $end_ts) {
        if (!empty($datelist))
            $datelist .= ',';
        $datelist .= "'" . gmdate('Y-m-d', $nextday) . "'";
        $nextday += 24*3600;
    }

    $qry_hits = "SELECT url, referrer, referrer_query_string as refer_query,
                        host, day, time
                   FROM {$table_prefix}stats 
                  WHERE ((day='" . gmdate('Y-m-d', $start_ts) . "' AND time>='" . gmdate('H:i:s', $start_ts) . "')
                     OR (day='" . gmdate('Y-m-d', $end_ts) .   "' AND time<'" . gmdate('H:i:s', $end_ts) . "')
                     " . ( (!empty($datelist)) ? " OR day IN ($datelist)) " : ')') . "
                     " . ((!empty($req_url)) ? " AND url='$req_url' " : "") . "
               ORDER BY $sortorder ";
    
    $pagehits = $wpdb->get_results($qry_hits);

    $alt = true;
    $total = 0;
    if ($pagehits) {
        foreach ($pagehits as $hit) {
            $referrer = $hit->referrer;
            if (strlen($referrer) > 40)
                $referrer = substr($referrer, 0, 45) . '...';
            if (!empty($hit->refer_query)) {
                $referrer .= '<br/>?' . substr($hit->refer_query, 0, 45);
                if (strlen($hit->refer_query) > 45)
                    $referrer .= '...';
            }
            $offset_time = strftime('%H:%M', strtotime($hit->time) + get_option('gmt_offset')*3600);
            echo ($alt ? '<tr class="alternate">' : '<tr>') . "\n";
            echo "<td class=\"desc\" title=\"{$hit->url}\">{$hit->url}</td>
                  <td class=\"desc\" title=\"{$hit->referrer}?{$hit->refer_query}\"><small>$referrer</small></td>
                  <td class=\"vers\" style=\"text-align: center;\">{$hit->host}</td>
                  <td class=\"vers\" style=\"text-align: center;\">$offset_time</td>
                  </tr>\n";
            $alt = !$alt;
        }
    }
    echo ($alt ? '<tr class="alternate">' : '<tr>') . "\n";
?>    

</table>

</div>
